package com.woquyin.controller.api;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.json.FastJson;
import com.jfinal.plugin.activerecord.Page;
import com.woquyin.common.Constance;
import com.woquyin.interceptor.ApiAuthInterceptor;
import com.woquyin.interceptor.ParamInterceptor;
import com.woquyin.model.BizTemplet;
import com.woquyin.model.BizUserCollect;
import com.woquyin.model.SecUser;
import com.woquyin.vo.ReturnData;
import com.woquyin.vo.collect.CollectAddVo;
import com.woquyin.vo.collect.CollectListVo;

/**
 * 用户相关的接口
 *
 * @author heem
 * @create time 创建时间：2017年2月10日 下午6:34:27
 * @version 1.0
 * @since
 */
@Before(ApiAuthInterceptor.class)
public class CollectController extends Controller {

	/**
	 * 查询
	 */
	public void list() {
		//String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
		//CollectListVo  data  = FastJson.getJson().parse(jsonString,  CollectListVo.class);
		//ReturnData<Page<BizTemplet>> resp = new ReturnData<Page<BizTemplet>>();
		//resp.setData(BizTemplet.dao.collectList(data.getPageNumber(), data.getToken()));
        SecUser loginUser = (SecUser)getSession().getAttribute("userInfo");
        if (loginUser != null) {
            setAttr("data", BizTemplet.dao.collectList(1, loginUser.getId()));
        }
		renderJsp("myLikeTemplate.jsp");
	}

	/**
	 * 新增
	 */
	public void add() {
		ReturnData<String> resp = new ReturnData<String>();
		String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
		CollectAddVo   data = FastJson.getJson().parse(jsonString, CollectAddVo.class);
		String token = data.getToken();
		String templetId = data.getTempletId();
		SecUser loginUser = (SecUser)getSession().getAttribute("userInfo");
		if (loginUser != null) {
			resp = BizUserCollect.dao.add(loginUser.getId(), templetId);
		}else{
			resp.setCode(Constance.WholeStateCodeEnum.OTHER_ERROR.getCode());
			resp.setMsg("收藏失败");
		}
		renderJson(resp);
	}


	public void delete() {
		ReturnData<String> resp = new ReturnData<String>();
		String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
		CollectAddVo vo = FastJson.getJson().parse(jsonString, CollectAddVo.class);
		SecUser loginUser = (SecUser)getSession().getAttribute("userInfo");
		if (BizUserCollect.dao.delete(loginUser.getId(), vo.getTempletId())) {
			resp.setMsg("删除成功");
		} else {
			resp.setCode(Constance.WholeStateCodeEnum.OTHER_ERROR.getCode());
			resp.setMsg("删除失败，请稍后再试");
		}
		renderJson(resp);
	}
}
